package com.hadoop.mapreduce.TextIndex;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class TextIndexTwo {
    static class TextIndexTwoMapper extends Mapper<IntWritable,Text,Text,Text>{
        Text k=new Text();
        Text v=new Text();
        @Override
        protected void map(IntWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] wordCount = value.toString().split("-");
            k.set(wordCount[0]);
            v.set(wordCount[1]);
            context.write(k,v);
        }
    }

    static class TextIndexTwoReducer extends Reducer<Text,Text,Text,Text>{
        Text v=new Text();
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuilder sb=new StringBuilder();
            for (Text text:
                 values) {
                sb.append(text.toString()+" ");
            }
            v.set(sb.toString());
            context.write(key,v);
        }
    }
    public static void main(String[] args) throws Exception {

        Configuration config = new Configuration();
        Job job = Job.getInstance(config);

        job.setMapperClass(TextIndexTwoMapper.class);
        job.setReducerClass(TextIndexTwoReducer.class);
        /*//当mapper与reducer的输出类型一致时可省略
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);*/
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        System.exit(job.waitForCompletion(true) ? 1:0);
    }
}
